package priv.pront.code.national.acwing.dp.tree;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-05-23 19:47
 */
public class Acwing1072_树的最长路径 {

    static int n, idx, ans;
    static int[] h, e, ne, w;
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        h = new int[n + 1];
        e = new int[2 * n + 1];
        ne = new int[2 * n + 1];
        w = new int[2 * n + 1];
        Arrays.fill(h, -1);
        for(int i = 1; i < n; i++){
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            int s = scanner.nextInt();
            add(a, b, s); add(b, a, s);
        }
        dfs(1, -1);
        System.out.println(ans);
    }

    private static int dfs(int u, int father){
        int dist = 0; // 从当前点向下走的最大长度
        int d1 = 0, d2 = 0;
        for(int i = h[u]; i != -1; i = ne[i]){
            int j = e[i];
            if(j == father) continue;
            int d = dfs(j, u) + w[i];
            dist = Math.max(d, dist);
            if(d >= d1){
                d2 = d1;
                d1 = d;
            }else if(d > d2){
                d2 = d;
            }
        }
        ans = Math.max(ans, d1 + d2);
        return dist;
    }

    private static void add(int a, int b, int s){
        e[idx] = b; w[idx] = s; ne[idx] = h[a]; h[a] = idx++;
    }
}
